<?php
/*
	本插件：$this->model('model')->方法名();
	其他地方调用：model('jy_xiaofeelian/model')->方法名();
*/
namespace jy_xiaofeelian;
defined('BY_JYA') or exit('error');
class red extends \plugin{
	public function gored($city=''){
		$red['set']=$this->plugin_setting('redset');
		if(!$red['set']['is_open']){
			return $red;
		}
		$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
		$day=date('Y-m-d');
		$timeids=$red['set']['timeids'];
		if($timeids){
			$timeids=str_array($timeids);
			$timelist=array();
			foreach($timeids as $key=>$hour){
				$startime=strtotime($day.' '.$hour);
				$timelist[]=array(
					'hour'=>$hour,
					'startime'=>$startime,
				);
			}
			paixu($timelist,'startime','asc');
			$timelist=array_values($timelist);
			$issendnum=0;
			foreach($timelist as $key=>&$item){
				$nkey=$key+1;
				if($timelist[$nkey]['startime']){
					$item['endtime']=$timelist[$nkey]['startime'];
				}else{
					$item['endtime']=strtotime($day.' 23:59');
				}
				if($item['startime']<TIMESTAMP && $item['endtime']>TIMESTAMP){
					$item['status']=1;
					$issendnum++;
					$nowsendlog=$item;
				}elseif($item['startime']>TIMESTAMP && $item['endtime']>TIMESTAMP){
					$item['status']=0;
				}elseif($item['endtime']<TIMESTAMP){
					$item['status']=2;
					$issendnum++;
				}
			}
		}
		if($nowsendlog){
			$lastlog=pdo_get('jy_xiaofeelian_sendredlog',array('cityid'=>$_SESSION['cityid'],'createtime between'=>array($nowsendlog['startime'],$nowsendlog['endtime']),'uniacid'=>$_SESSION['uniacid']),'','createtime desc');
		}
		if($nowsendlog){
			if(!$lastlog['id']){
				$lastlog=$this->addsendredlog($city,$red['set']);
			}
		}else{
			pdo_update('jy_xiaofeelian_sendredlog',array('status'=>1),array('cityid'=>$_SESSION['cityid'],'status'=>0,'uniacid'=>$_SESSION['uniacid']));
		}
		$pindex = max(1, intval($_RQ['page']));
		$psize = 20;
		$total='';
		$params=array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'cityid'=>$_SESSION['cityid'],'isjian'=>0);
		$list=pdo_getslice('jy_xiaofeelian_redlog',$params,array($pindex,$psize),$total,$fields,'','id desc');
		if($list){
			foreach($list as &$item){
				$item['member']=cfc('core')->member_show($item['mid']);
				$item['typetitle']=$this->jiangtype[$item['type']];
				$item['createtime']=date('Y-m-d H:i:s',$item['createtime']);
			}
		}
		$red['list']=$list;
		
		$pindex = max(1, intval($_RQ['page']));
		$psize = 10;
		$total='';
		$params=array('uniacid'=>$_SESSION['uniacid'],'cityid'=>$_SESSION['cityid'],'isjian'=>0);
		$logs=pdo_getslice('jy_xiaofeelian_redlog',$params,array($pindex,$psize),$total,$fields,'','id desc');
		$tips='';
		foreach($logs as $lv){
			$nickname=pdo_getcolumn('core_members',array('id'=>$lv['mid'],'uniacid'=>$_SESSION['uniacid']), 'nickname');
			$time=date('Y-m-d H:i:s',$lv['createtime']);
			$reward_name=$this->jiangtype[$lv['type']];
			$tips .=$nickname.'[抽到《'.$reward_name.'》,时间:'.$time.']；';
		}
		$red['tips']=$tips;
		$red['issendnum']=$issendnum;
		$red['getrednum']=pdo_count('jy_xiaofeelian_redlog',array('cityid'=>$_SESSION['cityid'],'mid'=>$_SESSION['uid'],'type'=>2,'uniacid'=>$_SESSION['uniacid']));
		$red['getallfee']=pdo_getsum('jy_xiaofeelian_redlog',array('redid'=>$lastlog['id'],'uniacid'=>$_SESSION['uniacid'],'type'=>2),'fee');
		$red['mygetallfee']=pdo_getsum('jy_xiaofeelian_redlog',array('cityid'=>$_SESSION['cityid'],'mid'=>$_SESSION['uid'],'type'=>2,'uniacid'=>$_SESSION['uniacid']),'fee');
		$red['lastfee']=price($lastlog['allfee']-$red['getallfee'],'int');
		$lastlog['allfee']=price($lastlog['allfee']);
		$red['sendred']=$lastlog;
		$red['timelist']=$timelist;
		$red['nowsendlog']=$nowsendlog?:array();
		return $red;
	}
	public function addsendredlog($city='',$info=array()){
		$allfee=0;
		$total=0;
		foreach($info['feeset'] as $iv){
			$allfee +=$iv['fee']*$iv['num'];
			$total +=$iv['num'];
		}
		$lastlog=array(
					'uniacid'=>$_SESSION['uniacid'],
					'city'=>$city,
					'cityid'=>$_SESSION['cityid'],
					'info'=>ijson_encode($info),
					'allfee'=>$allfee,
					'total'=>$total,
					'status'=>0,
					'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_xiaofeelian_sendredlog',$lastlog);
		$lastlog['id']=pdo_insertid();
		return $lastlog;
	}
	public function getred($uid=0,$sendredlogid=0){
			$uid=$uid?:$_SESSION['uid'];
			$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
			$redset=$this->plugin_setting('redset');
			$sendlog=pdo_get('jy_xiaofeelian_sendredlog',array('id'=>$sendredlogid,'uniacid'=>$_SESSION['uniacid']));
			$info=json_decode($sendlog['info'],true);
			$jiangset=$info['jiangset'];
			$feeset=$info['feeset'];
			$jifenset=$info['jifenset'];
			$isget=pdo_count('jy_xiaofeelian_redlog',array('createtime >='=>$daytime,'mid'=>$uid,'uniacid'=>$_SESSION['uniacid']));
			if($isget>=$redset['cannum']){
				exi('很遗憾！该场抽奖机会已用完！','error');
			}
			$jiangs = array();
			foreach($jiangset as $key =>$value){
				$jiangs[$key] = $value['rate'];
			}
			$jiangkey = $this->getRand($jiangs);
			$jiangitem=$jiangset[$jiangkey];
			if($jiangitem['type']==1){
				$temreward = array();
				foreach($jifenset as $key =>$value){
					if(!$value['isnum'] || $value['num']>$value['isnum']){
						$temreward[$key] = $value['num'];
					}
				}
				if(count($temreward)<=0){
					exi('很遗憾！积分红包已抽完！','error');
				}
				$infokey = $this->getRand($temreward);
				$fee=$jifenset[$infokey]['fee'];
				$this->addredlog($sendlog,$fee,$infokey,$uid,1);
				$jiangset[$infokey]['isnum'] +=1;
				$info['jiangset']=$jiangset;
				$jiangitem['fee']=$fee;
				pdo_update('jy_xiaofeelian_sendredlog',array('info'=>ijson_encode($info)),array('id'=>$sendredlogid,'uniacid'=>$_SESSION['uniacid']));
				$this->upmyfee($sendlog,$uid,$fee,1,0);
			}elseif($jiangitem['type']==2){
				$temreward = array();
				foreach($feeset as $key =>$value){
					if(!$value['isnum'] || $value['num']>$value['isnum']){
						$temreward[$key] = $value['num'];
					}
				}
				if(count($temreward)<=0){
					exi('很遗憾！消费金红包已抽完！','error');
				}
				$infokey = $this->getRand($temreward);
				$fee=$feeset[$infokey]['fee'];
				$this->addredlog($sendlog,$fee,$infokey,$uid,2);
				$feeset[$infokey]['isnum'] +=1;
				$info['feeset']=$feeset;
				$jiangitem['fee']=$fee;
				pdo_update('jy_xiaofeelian_sendredlog',array('info'=>ijson_encode($info)),array('id'=>$sendredlogid,'uniacid'=>$_SESSION['uniacid']));
				$this->upmyfee($sendlog,$uid,$fee,2,0);
			}elseif($jiangitem['type']==3 || $jiangitem['type']==4){
				if($jiangitem['couponid']){
					model('jy_weishop/coupon')->getcoupon($jiangitem['couponid'],$_SESSION['uid'],1);
				}
			}else{
				exi('奖品不存在，请联系客服完善奖品设置！','error');
			}
			return $jiangitem;
	}
	public function addmyfee($cityid=0,$uid=0,$city=''){
		$uid=$uid?:$_SESSION['uid'];
		$myfee=pdo_get('jy_xiaofeelian_myfee',array('mid'=>$uid,'cityid'=>$cityid,'uniacid'=>$_SESSION['uniacid']));
		if(!$myfee['id']){
			$myfee=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$uid,
				'cityid'=>$cityid,
				'city'=>$city,
				'createtime'=>TIMESTAMP
			);
			$myfee['fee']=0;
			pdo_insert('jy_xiaofeelian_myfee',$myfee);
			$myfee['id']=pdo_insertid();
		}
		return $myfee;
	}
	public function upmyfee($sendlog=array(),$uid=0,$fee=0,$type=0,$isjian=0){
		$uid=$uid?:$_SESSION['uid'];
		$this->addmyfee($sendlog['cityid'],$uid,$sendlog['city']);
		$update=array();
		if(!$isjian){
			if($type==2){
				$update['fee +=']=$fee;
			}else{
				$update['jifen +=']=$fee;
			}
		}else{
			if($type==2){
				$update['fee -=']=$fee;
			}else{
				$update['jifen -=']=$fee;
			}
		}
		pdo_update('jy_xiaofeelian_myfee',$update,array('mid'=>$uid,'cityid'=>$sendlog['cityid'],'uniacid'=>$_SESSION['uniacid']));
		return true;
	}
	public function addredlog($sendlog=array(),$fee='',$infokey='',$uid='',$type=0){
		$uid=$uid?:$_SESSION['uid'];
		$redlog=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$uid,
					'redid'=>$sendlog['id'],
					'cityid'=>$sendlog['cityid'],
					'city'=>$sendlog['city'],
					'fee'=>$fee,
					'infokey'=>$infokey,
					'type'=>$type,
					'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_xiaofeelian_redlog',$redlog);
		return $true;
	}
	public function hexiao($post=array()){
		$myfee=pdo_get('jy_xiaofeelian_myfee',array('id'=>$post['myfeeid'],'uniacid'=>$_SESSION['uniacid']));
		if(empty($myfee)){
			exi('核销码错误,无法核销','error');   
		}
		$post['paymoney']=$post['paymoney']>0?$post['paymoney']:0;
		$post['jifen']=$post['jifen']>0?$post['jifen']:0;
		if(empty($post['paymoney']) && empty($post['jifen'])){
			exi('请输入核销额度！','error');   
		}
		if($myfee['fee']<$post['paymoney'] && $post['paymoney']){
			exi('账号消费金不足，不能核销！余额：'.$myfee['fee'],'error');   
		}
		if($myfee['jifen']<$post['jifen'] && $post['jifen']){
			exi('账号积分不足，不能核销！余额：'.$myfee['jifen'],'error');   
		}
		pdo_update('jy_xiaofeelian_myfee',array('fee -='=>$post['paymoney'],'jifen -='=>$post['jifen']),array('id'=>$post['myfeeid'],'uniacid'=>$_SESSION['uniacid']));
		if($post['paymoney']>0){
			$redlog=array(
						'uniacid'=>$_SESSION['uniacid'],
						'mid'=>$myfee['mid'],
						'cityid'=>$myfee['cityid'],
						'city'=>$myfee['city'],
						'fee'=>$post['paymoney'],
						'merchid'=>$post['merchid'],
						'isjian'=>1,
						'type'=>2,
						'remark'=>'核销抵扣',
						'createtime'=>TIMESTAMP
			);
			pdo_insert('jy_xiaofeelian_redlog',$redlog);
		}
		if($post['jifen']>0){
			$redlog=array(
						'uniacid'=>$_SESSION['uniacid'],
						'mid'=>$myfee['mid'],
						'cityid'=>$myfee['cityid'],
						'city'=>$myfee['city'],
						'fee'=>$post['jifen'],
						'merchid'=>$post['merchid'],
						'isjian'=>1,
						'type'=>1,
						'remark'=>'核销抵扣',
						'createtime'=>TIMESTAMP
			);
			pdo_insert('jy_xiaofeelian_redlog',$redlog);
		}
		return true;
	}
	private function getRand($proArr){
		$result = '';
		$proSum = array_sum($proArr);
		foreach ($proArr as $key => $proCur) {
			$randNum = mt_rand(1, $proSum);
			if ($randNum <= $proCur) {
				$result = $key;
				break;
			}
			$proSum -= $proCur;
		}
		unset($proArr);
		return intval($result);
	}
}
?>